package cn.china.xfood.controller;

import cn.china.xfood.service.MainService;
import cn.china.xfood.service.OrderService;
import cn.china.xfood.service.UserMenuService;
import entity.Product;
import entity.Usermenu;
import entity.Userorder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import mapper.UsermenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Api(tags = "订单相关")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    UserMenuService userMenuService;


    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("userOrder")
    @ApiOperation("用户提交订单并清空购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",defaultValue = "admin")

})
    public String userOrder(HttpServletResponse res,  String username){
        res.setHeader("Access-Control-Allow-Origin" ,"*");

        List<Product> list = redisTemplate.opsForList().range(username, 0, redisTemplate.opsForList().size(username));

        Double totalPrice = 0.0;

        Boolean flag =true;

        long ts = System.currentTimeMillis();
        String s = String.valueOf(ts);

        for (int i = 0; i < list.size(); i++) {
            Product product = list.get(i);

            totalPrice = totalPrice + product.getProductPrice() * product.getProductAmount();
        }

        Date date = new Date(); // this object contains the current date value
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(date);

        Userorder userorder = new Userorder();

        userorder.setUsername(username);
        userorder.setTotalPrice(totalPrice);
        userorder.setOrderTime(format);
        userorder.setOrderNumber(s);

        int a = orderService.insert(userorder);
        if (a<1){
            flag=false;
        }

        for (int i = 0; i < list.size(); i++) {

            Product product = list.get(i);
            Usermenu usermenu = new Usermenu(s,product.getId(),product.getProductAmount(),product.getProductPrice());
            int b = userMenuService.insert(usermenu);
            if (b<1){
                flag=false;
            }
        }


        if (flag){
            return "提交成功";
        }else {
            return "error";
        }

    }

    @RequestMapping("clearOrder")
    @ApiOperation("用户主动清空购物车")
    public String clearOrder(HttpServletResponse res, @RequestParam String username) {
        res.setHeader("Access-Control-Allow-Origin", "*");

        redisTemplate.opsForList().getOperations().delete(username);

        return "ok";

    }

    @RequestMapping("getAllUserOrders")
    public Map<String,Object> getAllUserOrders(HttpServletResponse res,@RequestParam int page,@RequestParam int limit) {
        res.setHeader("Access-Control-Allow-Origin", "*");

        int Page = (page - 1) * limit;

        HashMap<String,Object> map = new HashMap<>();

        int i = orderService.getCount();

        List<Userorder> list = orderService.getAll(Page, limit);


        System.out.println(list);

        map.put("code",0);
        map.put("msg","");
        map.put("count",i);
        map.put("data",list);

        return map;

    }

    @RequestMapping("getHistory")
    public Map<String,Object> getHistory(HttpServletResponse res,@RequestParam String userStatus ,@RequestParam int page,@RequestParam int limit) {
        res.setHeader("Access-Control-Allow-Origin", "*");

        int Page = (page - 1) * limit;

        HashMap<String,Object> map = new HashMap<>();

        int i = orderService.getCount();

        List<Userorder> list = orderService.selectByUsernam(userStatus, Page, limit);

        map.put("code",0);
        map.put("msg","");
        map.put("count",i);
        map.put("data",list);

        return map;


    }





}
